﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class CompanyTypeEditControl : YuanlinUserControl,
        IReplyConverter<CompanyTypeReply, GetOperation>
    {
        protected ValuedTextBox titleValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;
        private CompanyType companyType;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetCompanyTypeRequest(this));
        }

        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if(companyType!=null)
            {
                this.titleValuedTextBox.Value = companyType.Title.Value;
            }   
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyTypeListUrl());
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (titleValuedTextBox.Value.Trim().Length == 0)
            {
                Globals.ShowMessage(this.Page, "CompanyTypeEditControl", "请输入名称！");
                return;
            }
            
            companyType.Title.Value =  CensorWordManager.CensorWord(this.titleValuedTextBox.Value);

            if (BusinessManager.UpdateCompanyType(companyType) == -2)
            {
                Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                return;
            }

            Page.Response.Redirect(SiteUrls.Instance().GetCompanyTypeListUrl());

        }

        #region IReplyConverter<CompanyTypeReply,GetOperation> Members

        public void ConvertReply(CompanyTypeReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                companyType = reply.Entity as CompanyType;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}